/*
 * VHPanelAssociation.java
 * Created on 27 sept. 2009
 *
 * VirtualHockey Internet Platform
 * http://code.google.com/p/virtualhockey/
 *
 * Copyright (c) 2005-2009, Jens and Jan Janke (VirtualHockey Project)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see http://www.gnu.org/licenses/.
 */

package com.hockeo.vhweb.client.ui.panels;

import com.extjs.gxt.ui.client.Style.LayoutRegion;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.TabPanelEvent;
import com.extjs.gxt.ui.client.util.Margins;
import com.extjs.gxt.ui.client.widget.HtmlContainer;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.TabPanel;
import com.extjs.gxt.ui.client.widget.layout.BorderLayout;
import com.extjs.gxt.ui.client.widget.layout.BorderLayoutData;
import com.hockeo.vhweb.client.VHMain;
import com.hockeo.vhweb.client.mvc.VHController;
import com.hockeo.vhweb.client.rpc.VHRpcCallback;
import com.hockeo.vhweb.client.rpc.VHRpcResultReceiver;
import com.hockeo.vhweb.client.ui.base.VHTabItem;
import com.hockeo.vhweb.client.ui.tabs.VHAssocSeasonOverviewTab;
import com.hockeo.vhweb.client.utils.VHJsData;

/**
 * Base panel for association info pages.
 *
 * @version $Id$
 * @author jjanke
 */
public class VHPanelAssociation extends LayoutContainer implements VHRpcResultReceiver<VHJsData>
{
  private static VHPanelAssociation d_instance;
  private VHController              d_controller;
  private String                    d_strAssocId;
  private VHJsData                  d_assoc;

  // UI elements
  //
  private static final String       HEADER_HTML_BASE         = "<div class='header'><table cellspacing='0' cellpadding='0'><tr><td style='padding-right: 4px;'><img src='ImageServlet?id={assoc_logo_id}'></td>"
                                                                 + "<td style='padding-left: 4px; padding-right: 60px; border-left: 1px solid #8db2e3;'>"
                                                                 + "<img src='images/flags/32x32/{assoc_id}.png' title='{assoc_name}'>"
                                                                 + "<br><span class='headerTitle'>{assoc_name}</span>"
                                                                 + "<br><span class='headerSubTitle'><table><tr>{member_countries}</tr></table></span></td>"
                                                                 + "</tr></table></div>";

  private static final String       MEMBER_COUNTRY_HTML_BASE = "<td title='{country_name}' style='padding: 0 2px;'><img src='images/flags/24x24/{country_id}.png'></td>";

  private HtmlContainer             d_htmlHeader;
  private TabPanel                  d_tabPanel;

  private VHAssocSeasonOverviewTab  d_tabItemSeasonOverview;

  /**
   * Creates a new panel to display association specific information.
   *
   * @param controller the display controller
   */
  private VHPanelAssociation( VHController controller )
  {
    d_controller = controller;

    setLayout( new BorderLayout() );
    setBorders( false );
    setAutoWidth( true );
    setLayoutOnChange( true );

    // header
    //
    d_htmlHeader = new HtmlContainer();

    BorderLayoutData data = new BorderLayoutData( LayoutRegion.NORTH );
    data.setMargins( new Margins( 4, 4, 4, 4 ) );
    data.setSize( 110f );
    add( d_htmlHeader, data );

    // tab panel
    //
    d_tabPanel = new TabPanel();
    d_tabPanel.setBorders( false );
    d_tabPanel.setAutoWidth( true );
    d_tabPanel.setTabScroll( true );

    data = new BorderLayoutData( LayoutRegion.CENTER );
    data.setMargins( new Margins( 0, 4, 4, 4 ) );
    add( d_tabPanel, data );

    // event listener to allow deferred content building for the individual tab items
    //
    d_tabPanel.addListener( Events.Select, new Listener<TabPanelEvent>() {
      public void handleEvent( TabPanelEvent evt )
      {
        ( (VHTabItem) evt.getItem() ).buildContent();
        ( (VHTabItem) evt.getItem() ).layout();
      }
    } );
  }

  /**
   * Called when an association has been loaded using RPC.
   */
  public void setRpcData( VHJsData assoc, String strPurpose )
  {
    loadAssociation( assoc );
  }

  /**
   * Customises the panel to display data for the given association.
   *
   * @param strAssocId the ID of the association for which to display data
   */
  private void loadAssociation( String strAssocId )
  {
    // If the panel is already configured to display data for the given assoc, we can skip
    // further actions
    //
    if ( d_strAssocId != null && d_strAssocId.equals( strAssocId ) )
      return;

    // Load association data via RPC
    //
    VHRpcCallback<VHJsData> callback = new VHRpcCallback<VHJsData>( this, null );
    VHController.getBaseDataService().getNationalAssociation( VHController.getLocale(), strAssocId, callback );
  }

  /**
   * Loads an association that already exists as mirrored server object.
   *
   * @param assoc the association to be displayed
   */
  private void loadAssociation( VHJsData assoc )
  {
    d_strAssocId = assoc.getId();
    d_assoc = assoc;

    String[] astrCountryCodes = d_assoc.getString( "memberCountries" ).split( "\\," );
    String[] astrCountryNames = d_assoc.getString( "memberCountries_translated" ).split( "\\," );

    // modify the association header
    //
    String strHtml = HEADER_HTML_BASE.replace( "{assoc_logo_id}", String.valueOf( d_assoc.getInt( "image100Id" ) ) );
    strHtml = strHtml.replace( "{assoc_id}", d_assoc.getString( "id" ) );
    strHtml = strHtml.replace( "{assoc_name}", d_assoc.getString( "id_translated" ) );

    StringBuilder sbMembers = new StringBuilder();
    for ( int i = 0; i < astrCountryCodes.length; i++ )
    {
      String strMemberHtml = MEMBER_COUNTRY_HTML_BASE.replace( "{country_id}", astrCountryCodes[i] );
      strMemberHtml = strMemberHtml.replace( "{country_name}", astrCountryNames[i] );

      sbMembers.append( strMemberHtml );
    }

    sbMembers.insert( 0, "<td>" + VHMain.getConstants().memberCountries() + "</td>" );
    strHtml = strHtml.replace( "{member_countries}", sbMembers.toString() );

    d_htmlHeader.setHtml( strHtml );

    // load tabs if they are not yet loaded or modify their content if they are
    //
    if ( d_tabItemSeasonOverview == null )
    {
      d_tabItemSeasonOverview = new VHAssocSeasonOverviewTab( d_controller, d_assoc );
      d_tabPanel.add( d_tabItemSeasonOverview );
    }
    else
    {
      d_tabPanel.setSelection( d_tabItemSeasonOverview );
      d_tabItemSeasonOverview.setAssociation( d_assoc );
    }
  }

  /**
   * Returns a panel to display association specific information.
   *
   * @param controller the display controller
   * @param strAssocId the ID of the association for which to build this panel
   */
  public static VHPanelAssociation get( VHController controller, String strAssocId )
  {
    if ( d_instance == null )
      d_instance = new VHPanelAssociation( controller );

    d_instance.loadAssociation( strAssocId );

    return d_instance;
  }
}
